package cn.dancepeak.mongo.controller;

import cn.dancepeak.artcle.service.IArticleServiceImpl;
import cn.dancepeak.article.domain.Article;
import cn.dancepeak.article.query.ArticleQuery;
import cn.dancepeak.exception.BusinessException;
import cn.dancepeak.mongo.Comment;
import cn.dancepeak.mongo.service.ICommentServiceImpl;
import cn.dancepeak.utils.R;
import cn.dancepeak.vo.ArticleTypeDateNumVo;
import cn.dancepeak.vo.ArticleTypeNameNumVo;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comment")
@Slf4j
public class CommentController {
    @Autowired
    private ICommentServiceImpl commentService;

    //增加一条评论
    @PostMapping("/add")
    public R<Void> add(@RequestBody Comment comment){
        try {
            commentService.add(comment);
            return R.ok("新增评论成功");
        } catch (BusinessException e) {
            log.info("新增评论异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("新增评论系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }
    //根据_ID删一条评论
    @DeleteMapping("/{_id}")
    public R<Void> delete(@PathVariable("_id") String _id){
        try {
            commentService.delete(_id);
            return R.ok("删除评论成功");
        } catch (BusinessException e) {
            log.info("删除评论异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("删除评论系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }
    //查评论
    @GetMapping("/findByArticleId/{articleId}")
    public R<List<Comment>> findByArticleId(@PathVariable("articleId") Long articleId){
        try {
            List<Comment> list = commentService.findByArticleId(articleId);
            return R.ok(list);
        } catch (BusinessException e) {
            log.info("查询评论异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.info("查询评论系统异常：{}",e.getMessage());
            return R.fail(e.getMessage());
        }
    }
}
